from sqlalchemy import LargeBinary
from extension import db
from datetime import datetime


class Book(db.Model):
    __tablename__ = 'book'
    bookId = db.Column(db.Integer, primary_key=True, autoincrement=True)
    bookName = db.Column(db.String, nullable=False)
    author = db.Column(db.String(255), nullable=False)
    ISBN = db.Column(db.String(255), nullable=False)
    category = db.Column(db.String(255), nullable=False)
    quantity = db.Column(db.Integer, nullable=False)
    owner = db.Column(db.String(255), nullable=False)
    description = db.Column(db.String(255), nullable=False)
    price = db.Column(db.DECIMAL(10, 2), nullable=False)
    press = db.Column(db.String(255), nullable=False)

    def serialize(self):
        return {
            'bookId': self.bookId,
            'bookName': self.bookName,
            'author': self.author,
            'ISBN': self.ISBN,
            'category': self.category,
            'quantity': self.quantity,
            'owner': self.owner,
            'description': self.description,
            'price': self.price,
            'press': self.press
        }


class Account(db.Model):
    __tablename__ = 'account'
    accountId = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(255), nullable=False)
    password = db.Column(db.String(255), nullable=False)
    phone = db.Column(db.String(255), nullable=False)
    address = db.Column(db.String(255), nullable=False)
    firstname = db.Column(db.String(255), nullable=False)
    lastname = db.Column(db.String(255), nullable=False)
    admin = db.Column(db.Integer, nullable=False)
    question = db.Column(db.String(255), nullable=False)
    answer = db.Column(db.String(255), nullable=False)
    money = db.Column(db.DECIMAL(10, 2), nullable=False)
    borrowBookNum = db.Column(db.Integer, nullable=False)


class Category(db.Model):
    __tablename__ = 'category'
    categoryId = db.Column(db.Integer, primary_key=True, autoincrement=True)
    categoryName = db.Column(db.String(255), nullable=False)


class Borrow(db.Model):
    __tablename__ = 'borrow'
    borrowId = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(255), nullable=False)
    bookName = db.Column(db.String(255), nullable=False)
    startDate = db.Column(db.DateTime, nullable=False, default=datetime.now)
    endDate = db.Column(db.DateTime, nullable=False, default=datetime.now)
    code = db.Column(db.String(1000), nullable=False)
    returnBook = db.Column(db.Integer, nullable=False)
    getBook = db.Column(db.Integer, nullable=False)
    evaluation = db.Column(db.Integer, nullable=False)


class NewBorrow(db.Model):
    __tablename__ = 'newBorrow'
    newBorrowId = db.Column(db.Integer, primary_key=True, autoincrement=True)
    month = db.Column(db.String(255), nullable=False)
    categoryName = db.Column(db.String(255), nullable=False)
    quantity = db.Column(db.Integer, nullable=False)


class Letter(db.Model):
    __tablename__ = 'letter'
    letterId = db.Column(db.Integer, primary_key=True, autoincrement=True)
    sender = db.Column(db.String(255), nullable=False)
    letter = db.Column(db.String(255), nullable=False)
    view = db.Column(db.Integer, nullable=False)
    recipient = db.Column(db.String(255), nullable=False)
    msgType = db.Column(db.Integer, nullable=False)
    sendTime = db.Column(db.DateTime, nullable=False)


class User(db.Model):
    __tablename__ = 'user'
    userId = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(255), nullable=False)
    password = db.Column(db.String(255), nullable=False)


class Chat(db.Model):
    __tablename__ = 'chat'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    chatId = db.Column(db.Integer, nullable=False)
    username = db.Column(db.String(1000), nullable=False)
    order = db.Column(db.Integer, nullable=False)
    question = db.Column(db.String(1000), nullable=False)
    answer = db.Column(db.String(1000), nullable=False)


class Recommend(db.Model):
    __tablename__ = 'recommend'
    recommendId = db.Column(db.Integer, primary_key=True, autoincrement=True)
    bookId = db.Column(db.Integer, nullable=False)
    bookName = db.Column(db.String(255), nullable=False)
    author = db.Column(db.String(255), nullable=False)
    getTime = db.Column(db.DateTime, nullable=False, default=datetime.now)


class Dialog(db.Model):
    __tablename__ = 'dialog'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    chatid = db.Column(db.Integer, nullable=False)
    question = db.Column(db.String(255), nullable=False)
    answer = db.Column(db.String(255), nullable=False)
    type = db.Column(db.Integer, nullable=False)
    img = db.Column(db.String(255), nullable=False)